/**
 * Filename: d:\CODE\FTP\FTP_TEST\FTP\static\js\page_3.js
 * Path: d:\CODE\FTP\FTP_TEST\FTP\static\js
 * Created Date: Wednesday, May 11th 2022, 4:20:55 pm
 * Author: xiguan
 * 
 * Copyright (c) 2022 XiGuan Inc.
 */


const basic_url = "http://127.0.0.1:8880";

let current_syncdir_id = 0; // 当前同步目录编号
let current_server_port = 22; //当前监听端口
let current_interclock_id = 0; //当前同步定时器id

// 封装ajax请求

function ajax(params){
    let url = basic_url+params.url;
    let data = params.data;
    let method = "GET";
    if(params.method !== ""){
        method = params.method;
    }

    return $.ajax({
        url : url,
        data : data,
        method: method
    });
}
async function get_all_ignore(){
    $(".content3-ignore-item").remove();
    let params = {
        url: "/ignore/get_all/"
    };
    // '+ignores[i].id+'
    await ajax(params).then(data => {
        let ignores = JSON.parse(data);
        for(let i=0; i<ignores.length; i++) {
            str =   '<div className="checkbox"  class="content3-ignore-item" > ' +
            '   <label> ' +
            '      <input type="checkbox" name="check" value="'+ignores[i].ignore+'" class="'+ignores[i].id+'">'  + ignores[i].name +
            '   </label> ' +
            '</div> ';
            if ((i+1)%8 === 0) {
                str = str + '<div></div>';
            }
            $('#content3').append(str);
            if(ignores[i].ignore === 1) {
                $("#content3 ." + ignores[i].id).attr("checked", true);
            } else {
                $("#content3 ." + ignores[i].id).attr("checked", false);
            }
        }
    });
}

$(async function() {
    console.info("hah4")
    await get_all_ignore();

    // 点击忽略列表中的新增按钮
    $("#content3-add-btn").click(async () => {
        var dialog = bootbox.dialog({
            title: '新增忽略列表',
            message: "忽略类型：<input type='text' id='content3-add-ignore' style='width: 515px;'/>",
            size: 'large',
            buttons: {
                cancel: {
                    label: "取消",
                },
                ok: {
                    label: "确认",
                    callback: async function(){
                        let params = {
                            url: "/ignore/add_ignore/",
                            data: {
                                "name": $("#content3-add-ignore").val(),
                            }
                        };
                        await ajax(params).then(async data => {
                            if (data === "OK") {
                                var dialog = bootbox.dialog({
                                    message: '<p class="text-center mb-0"><i class="fa fa-spin fa-cog"></i> 正在新增忽略列表...</p>',
                                    closeButton: false
                                });
                                setTimeout(async function (){
                                    dialog.modal('hide');
                                    await get_all_ignore();
                                },2000);

                            }
                        })
                    }
                }
            }
        })
    });

    $("#content3-save-btn").click(() => {
        let ignores = {};
        $("input[name='check']").each(function() {
            if($(this)[0].checked === true) {
                ignores[$(this).attr("class")] = 1;
            } else {
                ignores[$(this).attr("class")] = 0;
            }

        });
        let params = {
            url: "/ignore/save_ignore/",
            data: {
                "ignores": JSON.stringify(ignores)
            }
        };
        ajax(params).then(async data => {
            await get_all_ignore();
            bootbox.alert({
                message: "保存成功！",
                buttons: {
                     ok: {
                        label: "确认",
                        className: "btn-primary",
                    }
                }
            });
        })
    });
})